// tspcluster - Traveling Salesman Problem
// Copyright (C) 2006-2007  Frederik Carlier
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

using System;
using System.Reflection;
using System.ServiceProcess;

namespace UGent.Tsp
{
    public static class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("tspcluster.starting");

            bool console; // if true, run on console, otherwise, run as service
            string algorithm; // name of the algorithm

            if (args.Length == 0)
            {
                // Assume server mode
                Console.WriteLine("Server mode assumed.");
                ServiceBase.Run(new ServiceBase[]{new RemotingServer(), new ProfileSwappers()});
                return;
            }
            else if (args.Length == 1)
            {
                console = true;
                algorithm = args[0];
            }
            else
            {
                console = args[0].ToLowerInvariant() == "console";
                algorithm = args[1];
            }

            Type type = Type.GetType(algorithm);
            
            if (console)
            {
                MethodInfo method = type.GetMethod("RunOnConsole", BindingFlags.Static | BindingFlags.Public);
                method.Invoke(null, null);
            }
            else
            {
                Object instance = Activator.CreateInstance(type);
                ServiceBase.Run((ServiceBase)instance);
            }
        }
    }
}